package cn.edu.seu.ssny.service.impl;

import cn.edu.seu.ssny.dao.IGatewayDao;
import cn.edu.seu.ssny.dao.support.IBaseDao;
import cn.edu.seu.ssny.entity.Enterprise;
import cn.edu.seu.ssny.entity.Gateway;
import cn.edu.seu.ssny.service.IGatewayService;
import cn.edu.seu.ssny.service.support.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 网关表
 */
@Service
public class GatewayServiceImpl extends BaseServiceImpl<Gateway, Integer> implements IGatewayService {

    @Autowired
    private IGatewayDao gatewayDao;

    @Override
    public IBaseDao<Gateway, Integer> getBaseDao() {
        return this.gatewayDao;
    }


    @Override
    public Gateway findByName(String name) {
        return gatewayDao.findByName(name);
    }

    @Override
    public Gateway findByIdentification(String identification) {
        return gatewayDao.findByIdentification(identification);
    }

    @Override
    public void saveOrUpdate(Gateway gateway, Enterprise enterprise) {
        if(gateway.getId() != null){
            Gateway dbGateway = find(gateway.getId());
            dbGateway.setName(gateway.getName());
            dbGateway.setIdentification(gateway.getIdentification());
            dbGateway.setDescription(gateway.getDescription());
            dbGateway.setVersion(gateway.getVersion());
            dbGateway.setCommunicationInterface(gateway.getCommunicationInterface());
            dbGateway.setAddress(gateway.getAddress());
            update(dbGateway);
        }else{
            gateway.setEnterprise(enterprise);
            save(gateway);
        }
    }

}